package com.hzcx.app.simplechat.ui.publicui;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.ui.chat.bean.GdMapPoiBean;
import com.hzcx.app.simplechat.ui.home.AddFriendSearchActivity;
import com.hzcx.app.simplechat.ui.publicui.adapter.LocationAdapter;
import com.hzcx.app.simplechat.ui.publicui.event.LocationEvent;
import com.hzcx.app.simplechat.util.app.AppHelper;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;
import com.hzcx.app.simplechat.util.map.GdMapUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 位置搜索
 */
public class LocationSearchActivity extends BaseActivity {

    public static final String INTENT_CITY = "INTENT_CITY";

    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.iv_clear)
    ImageView ivClear;
    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.rv_location)
    RecyclerView rvLocation;

    private List<GdMapPoiBean> list;
    private LocationAdapter locationAdapter;

    @Override
    public int getLayout() {
        return R.layout.activity_location_search;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
        // 弹出软键盘
        AppHelper.showSoftKeyboard(etSearch, this);

        list = new ArrayList<>();
        locationAdapter = new LocationAdapter(list);
        rvLocation.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        rvLocation.setAdapter(locationAdapter);

        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (etSearch.getText().toString().length() > 0) {
                    ivClear.setVisibility(View.VISIBLE);
                    searchAddress();
                } else {
                    ivClear.setVisibility(View.GONE);
                    list.clear();
                    locationAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        etSearch.setOnEditorActionListener((textView, i, keyEvent) -> {
            if (i == EditorInfo.IME_ACTION_SEARCH) {
                if (EmptyUtils.isEmpty(etSearch.getText().toString())) {
                    return false;
                }
                searchAddress();
                return true;
            }
            return false;
        });
    }

    @Override
    public void initData() {
        locationAdapter.setOnItemClickListener((adapter, view, position) -> {
            EventBus.getDefault().post(new LocationEvent(list.get(position).getLat(), list.get(position).getLng(),
                    getIntent().getStringExtra(INTENT_CITY)+"·"+list.get(position).getTitle()));
            finish();
        });
    }

    private void searchAddress() {
        GdMapUtils.getInstance().getAddressList(etSearch.getText().toString(), getIntent().getStringExtra(INTENT_CITY), new PoiSearch.OnPoiSearchListener() {
            @Override
            public void onPoiSearched(PoiResult poiResult, int i) {
                if (i == 1000) {
                    if (poiResult != null && poiResult.getQuery() != null) {
                        if (poiResult.getQuery().equals(GdMapUtils.getInstance().getQuery())) {
                            list.clear();
                            for (PoiItem item : poiResult.getPois()) {
                                GdMapPoiBean bean = new GdMapPoiBean();
                                bean.setLat(item.getLatLonPoint().getLatitude());
                                bean.setLng(item.getLatLonPoint().getLongitude());
                                bean.setTitle(item.getTitle());
                                bean.setAddress(item.getAdName() + item.getSnippet());
                                list.add(bean);
                            }
                            locationAdapter.notifyDataSetChanged();
                            hideLoading();
                        }
                    }
                }
            }

            @Override
            public void onPoiItemSearched(PoiItem poiItem, int i) {

            }
        });
    }
}
